import { getCourselist, getCategorylist, getCourseDetail, collect, delcollect, buyCourse, courseOrderList } from '@/api/course/course';
import * as types from '../types'
import Vue from 'vue'
export default {
    namespaced: true,
    state: {
        courseList: [],
        categoryId: '',
        categoryList: [],
        courseOrderList: [],
        cartList: {},
        size: 6,
        lastId: ''
    },
    mutations: {
        [types.SET_COURSE_LIST](state, list) {
            // 设置获取最后一个ID
            state.lastId = list[list.length-1]._id
            state.courseList = list.concat(state.courseList)
        },
        [types.EMPTY_COURSE_LIST](state){
            state.lastId = ""
            state.courseList = []
        },
        [types.SET_CATEGORYID](state, categoryId) {
            state.categoryId = categoryId
        },
        [types.SET_CATEGORY_LIST](state, categoryList) {
            state.categoryList = categoryList
        },
        [types.SET_ORDER_COURSE](state,courseOrderList){
            state.courseOrderList = courseOrderList 
        },
        [types.ADD_CARLIST](state,course){
            state.cartList[course._id] = course
        },
        [types.EMPTY_CARLIST](state){
            state.cartList={}
        },
        [types.REMOVE_CARLIST](state,courseId){
            Vue.delete(state.cartList,courseId)
        }

    },
    getters: {
        // 课程列表
        courseList: (state) => {
            if (state.categoryId.length > 0) {
                return state.courseList.filter(x => x.category == state.categoryId)
            } else {
                return state.courseList
            }
        },
        // 分类列表
        categoryList: (state) => {
            let first = [{
                text: '全部课程',
                value: ''
            }]
            let newList = state.categoryList.map(x => ({ text: x.name, value: x._id }))
            return [first.concat(newList)]
        },
        orderList:(state)=>{
            return state.courseOrderList.map(x=>x.course)
        }
    },
    actions: {
        // 获取所有课程列表
        async [types.GET_COURSE_LIST]({ commit },query={id:undefined,size:5}) {
            let{id,size} = query
            let res = await getCourselist(id,size)
            if (res.code === 0) {
                // 获取课程列表成功
                if(res.data.length==0){
                    return false
                }else{
                    commit(types.SET_COURSE_LIST, res.data)
                    return res.data
                }
            } else {
                // 获取失败
                return Promise.reject(res.data)
            }
        },
        //获取课程详情
        async [types.GET_COURSE_DETAIL]({ commit }, id) {
            let res = await getCourseDetail(id)
            if (res.code === 0) {
                // 获取课程详情
                return res.data
            } else {
                // 获取失败
                return Promise.reject(res.data)
            }
        },
        //收藏
        async [types.COLLECT]({ commit }, id) {
            let res = await collect(id)
            if (res.code === 0) {
                return res.data
            } else {
                return Promise.reject(res.data)
            }
        },
        // 取消收藏
        async [types.DEL_COLLECT]({ commit }, id) {
            let res = await delcollect(id)
            if (res.code === 0) {
                return res.data
            } else {
                return Promise.reject(res.data)
            }
        },
        // 获取收藏列表
        async [types.GET_CATEGORY_LIST]({ commit }, id) {
            let res = await getCategorylist(id)
            if (res.code === 0) {
                // 设置分类列表
                commit(types.SET_CATEGORY_LIST, res.data)
            } else {
                // 获取失败
                return Promise.reject(res.data)
            }
        },
        //获取购买课程列表
        async [types.GET_COURSE_ORDER_LIST]({commit},id){
            let res = await courseOrderList()
            if(res.code === 0 ){
                // 设置课程列表
                commit(types.SET_ORDER_COURSE,res.data)
            }else {
                return Promise.reject(res.data)
            }
        },
        // 购买课程动作
        async [types.BUY_COURSE]({ commit }, id) {
            let res = await buyCourse(id)
            if (res.code === 0) {
                commit(types.SET_ORDER_COURSE,res.data)
                return res.data
            } else {
                return Promise.reject(res.data)
            }
        }
    }
}